//
//  HMSportSpeaker.m
//  XRSport
//
//  Created by acer on 16/10/28.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMSportSpeaker.h"
#import <AVFoundation/AVFoundation.h>
#import "NSString+HMNumberConvert.h"
@implementation HMSportSpeaker
{
    //运动类型
    NSString *_playText;
//    上次运动的距离
    double _lastDistance;
    //语音播放器
    AVPlayer *_audioPlayer;
//    语音合成器
    AVSpeechSynthesizer *_speechSynthesizer;
//    声音字典
    NSDictionary *_voiceDict;
    //语音播放数组
    NSMutableArray *_voiceArrayM;
}
-(instancetype)init
{
    if (self = [super init]) {
        _unitDistance = 0.5;
        _lastDistance = 0;
        _audioPlayer = [AVPlayer new];
        _speechSynthesizer = [AVSpeechSynthesizer new];
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"voice.json" withExtension:nil subdirectory:@"voice.bundle"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        _voiceDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        //注册通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playItemDidEnd:) name:HMSportNotificationGPSSignal object:nil];
    }
    return self;
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)playItemDidEnd:(NSString *)string
{
//    NSLog(@"播放完成");
    [self playFirstVoice];
}
-(void)sportState:(HMSportState)state
{
    NSString *text;
    switch (state) {
        case HMSportStateContinue:
            text = @"运动已恢复";
            break;
        case HMSportStatePause:
            text = @"运动已暂停";
            break;
        case HMSportStateFinish:
            text = @"放松一下吧";
            break;
        default:
            break;
    }
    [self playAudioWtih:text];
}
-(void)startSportWithType:(HMSportType)type
{
    switch (type) {
        case HMSportTypeRun:
            _playText = @"跑步";
            break;
        case HMSportTypeRide:
            _playText = @"骑行";
            break;
        case HMSportTypeWalk:
            _playText = @"走路";
            break;
        default:
            break;
    }
    NSString *str = [NSString stringWithFormat:@"开始%@",_playText];
    [self playAudioWtih:str];
}
-(void)reportWithDistance:(double)distance time:(NSTimeInterval)time speed:(double)speed
{
    if (distance < _lastDistance + _unitDistance) {
        return;
    }
    _lastDistance = (NSUInteger)(distance / _unitDistance) * _unitDistance;
    NSString *str = @"你已经 %@ %@ 公里 用时 %@ 平均速度 %@ 公里每小时 太棒了";
    NSString *distanceStr = [NSString hm_numberStringWithNumber:distance hasDotNumber:YES];
    NSString *timeStr = [NSString hm_timeStringWithTimeValue:time];
    NSString *speedStr = [NSString hm_numberStringWithNumber:speed hasDotNumber:YES];
    NSString *text = [NSString stringWithFormat:str,_playText,distanceStr,timeStr,speedStr];
    [self playAudioWtih:text];
}
-(void)playAudioWtih:(NSString *)text
{
//    NSLog(@"播放语音内容 - %@", text);
    NSArray *array = [text componentsSeparatedByString:@" "];
    _voiceArrayM = [NSMutableArray arrayWithArray:array];
    [self playFirstVoice];
    
    /*
    [_speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:text];
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    utterance.voice = voice;
    
    [_speechSynthesizer speakUtterance:utterance];
     */
}
-(void)playFirstVoice
{
    if (_voiceArrayM.count == 0) {
//        NSLog(@"没有可播放的内容");
        return;
    }
    NSString *str = _voiceArrayM.firstObject;
    NSString *string = _voiceDict[str];
    [_voiceArrayM removeObjectAtIndex:0];
    NSURL *url = [[NSBundle mainBundle] URLForResource:string withExtension:nil subdirectory:@"voice.bundle"];
    // 判断 URL 是否存在
    if (url == nil) {
        NSLog(@"%@ 对应的 mp3 没有找到", string);
        return;
    }
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
    [_audioPlayer replaceCurrentItemWithPlayerItem:item];
    [_audioPlayer play];
}
@end
